<!-- BEGIN PAGE HEADER-->
<style>
    .ibox label {
        margin-left: 8px;
        margin-bottom: 8px;
    }

    .page-bar .pull-right {
        display: none;
    }
</style>
<?php $this->load->view('partials/breadcrumb.tpl') ?>

<div class="row">
    <div class="col-xs-12">
        <!-- Begin: life time stats -->
        <div class="portlet">
            <?= form_open('', Array('class' => 'form-inline', 'role' => 'form')) ?>
            <?= search_form_input('order_no', filterValue('order_no'), '订单号（自发货）', 'class="form-control input-xs"'); ?>
            <?= search_form_input('shipping_no', filterValue('shipping_no'), '运单号（自发货）', 'class="form-control input-xs"'); ?>
            <?= search_form_dropdown('shop_id', array('' => '选择账户...') + $allShops, filterValue('shop_id'), '所属账户', 'class="form-control input-xs select2me new-select"'); ?>
            <?= search_form_dropdown('product_id', array('' => '选择商品...') + $allProducts, filterValue('product_id'), '选择商品', 'class="form-control input-xs select2me new-select"'); ?>
            <?= search_form_dropdown('type_mark', array('' => '选择出库方式...') + IOSS_StaticConfig::getShippingOutType(), filterValue('type_mark'), '出库方式', 'class="form-control input-xs"'); ?>
            <?= search_form_dropdown('location', array('' => '选择所在仓库...') + IOSS_StaticConfig::getShippingWarehouse(), filterValue('location'), '选择所在仓库', 'class="form-control input-xs"'); ?>

            <div style="clear:both;height:14px;"></div>
            <?= search_form_input('record_number_perpage', filterValue('record_number_perpage'), '每页显示条数', 'class="form-control input-xs"'); ?>
            <?= search_form_dropdown('record_order_type', array('' => '排序方式...') + MStorageRecord::getOrderType(), filterValue('record_order_type'), '排序', 'class="form-control input-xs"'); ?>
            <div class="input-group input-large date-picker input-daterange" data-date="" data-date-format="yyyy-mm-dd">
                <input type="text" class="form-control" name="record_create_time_begin"
                       value="<?= filterValue('record_create_time_begin') ?>" placeholder="开始时间" autoComplete="off">
                <span class="input-group-addon">to </span>
                <input type="text" class="form-control" name="record_create_time_end"
                       value="<?= filterValue('record_create_time_end') ?>" placeholder="结束时间" autoComplete="off">
            </div>

            <button type="button" class="btn default" id="reset">重置条件</button>
            <button type="submit" class="btn green about_search">搜索 <i class="fa fa-search"></i></button>
            <button type="button" class="btn blue"  id="deliver">打包发货</button>

            <?= form_close() ?>
            <div class="portlet-body">
                <div class="row">
                    <div class="col-xs-12">
                        <div class="table-scrollable">
                            <form action="<?= site_url('storage/shipping/add') ?>" method="post"  id="app-ids">
                                <table class="table table-striped table-bordered table-hover">
                                    <thead class="flip-content">
                                    <tr>
                                        <th width="1%">
                                            <label>
                                                <input type="checkbox" id="merge_all" ">
                                            </label>
                                        </th>
                                        <th width="1%">ID</th>
                                        <th width="8%">商品名称</th>
                                        <th width="4%">单价</th>
                                        <th width="3%">数量</th>
                                        <th width="4%">总计</th>
                                        <th width="4%">责任人</th>
                                        <th width="5%">采购人</th>
                                        <th width="5%">出库方式</th>
                                        <th width="8%">出库商品批次</th>
                                        <th width="4%">仓库</th>
                                        <th width="4%">仓库区</th>
                                        <th width="8%">所属账户</th>
                                        <th width="6%">采购时间</th>
                                        <th width="5%">状态</th>
                                        <th width="8%">备注</th>
                                        <th width="8%">创建时间</th>
                                        <th width="12%">操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <?php foreach ($list as $v) { ?>
                                        <tr>
                                            <td>

                                                <input type="checkbox" class="mbox" name="apply_ids[]"
                                                       value="<?= $v->id ?>" title="ID:<?= $v->id ?>">
                                            </td>
                                            <td><?= $v->id ?></td>
                                            <td><?= htmlspecialchars($v->product_name) . '(id:' . $v->product_id . ')' ?></td>
                                            <td><?= htmlspecialchars($v->price) ?></td>
                                            <td><?= htmlspecialchars($v->quantity) ?></td>
                                            <td><?= htmlspecialchars($v->amount) ?></td>
                                            <td><?= htmlspecialchars($allPurchaser[$v->purchaser_id]) ?></td>
                                            <td><?= htmlspecialchars($v->related_person) ?></td>
                                            <td <?php if ($v->mark == 7): ?>class="text-center popovers"
                                                data-container="body" data-trigger="hover" data-placement="top"
                                                data-content="<?= $v->order_no ?>"<?php endif; ?>><?= IOSS_StaticConfig::getShippingType($v->type_mark) ?></td>
                                            <td><?= htmlspecialchars($v->warehousing) ?></td>
                                            <td><?= $v->location ? IOSS_StaticConfig::getShippingWarehouse($v->location) : '未入库' ?></td>
                                            <td><?= $v->area ? IOSS_StaticConfig::getShippingBlock($v->area) : '未入库' ?></td>
                                            <td><?= htmlspecialchars($allShops[$v->shop_id]) ?></td>
                                            <td><?= date('Y-m-d', $v->purchase_time) ?></td>

                                            <td><?= MStorageRecord::getState($v->status) ?></td>
                                            <td><?= htmlspecialchars($v->remark) ?></td>
                                            <td><?= date('Y-m-d H:i', $v->create_time) ?></td>
                                            <td class="numeric text-center">
                                                <?php if (!$v->type && $v->status == 1 && !$v->has_deliver): ?>
                                                    <button type="button" data-id="<?= $v->id ?>" class="btn btn-sm purple record-edit">编辑
                                                    </button>
                                                    <a href="javascript:void(defConfirm('<?=site_url($thisModule.'warehouse/out_fail/'.$v->id) ?>'));" class="btn btn-sm red ">作废</a>
                                                <?php endif; ?>
                                                <?php if (!$v->type && $v->has_deliver && $v->status): ?>
                                                    <span class="label label-success">已发货</span>
                                                <?php endif; ?>
                                                <?php if (!$v->type &&  !$v->status): ?>
                                                    <span class="label label-warning">已作废</span>
                                                <?php endif; ?>
                                            </td>
                                        </tr>
                                    <?php } ?>
                                    </tbody>
                                </table>
                            </form>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-xs-12 text-right">
                        <?= $pagination ?>
                    </div>
                </div>
            </div>
        </div>
        <!-- End: life time stats -->
    </div>
</div>

<script>
    var recordEdit = $(".record-edit");
    recordEdit.click(function () {
        var record_id = this.dataset.id;
        $.ajax({
            url: "<?= site_url('storage/warehouse/ajaxDetail') ?>",
            method: "POST",
            data: {'id': record_id},
            dataType: "json",
            success: function (res) {
                document.getElementById("form_edit").action = "<?= site_url('/storage/warehouse/edit/') ?>"+'/'+record_id;
                $(".product_name").text(res.product_name);
                set_select_checked('shop-id',res.shop_id);
                set_select_checked('type_mark',res.type_mark);
                set_select_checked('location',res.location);
                set_select_checked('area',res.area);
                $("input[name='quantity']").val(res.quantity);
                $("input[name='order_no']").val(res.order_no);
                $("input[name='amount']").val(parseFloat(res.amount));
                $("input[name='status']").val(parseFloat(res.status));
                $("input[name='purchase_time']").val(res.purchase_time);
                $("textarea[name='remark']").val(res.remark);
                if (res.type == 0 && res.has_deliver == 1){
                    $(".deliver-need").fadeIn();
                }
            },
            fail: function (res) {
                console.log(res);
            }
        });
        $(".editModal").modal('show');
    });

    function set_select_checked(selectId, checkValue){
        var select = document.getElementById(selectId);
        for (var i = 0; i < select.options.length; i++){
            if (select.options[i].value == checkValue){
                select.options[i].selected = true;
                break;
            }
        }
    }
</script>

<!--  edit modal  -->
<div class="modal fade editModal">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <?= form_open(site_url(''), Array('class' => 'form-horizontal', 'role' => 'form', 'id' => 'form_edit')) ?>
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">×</span></button>
                <h4 class="modal-title" id="gridModalLabel">编辑</h4>
            </div>
            <div class="modal-body">
                <div class="form-group"><label class="col-xs-3 control-label">商品：</label>
                    <div class="col-xs-7 product_name">

                    </div>
                </div>
                <div class="deliver-need" style="display: none;">
                    <?= edit_form_input('shipping_no', set_value('shipping_no'), '物流单号') ?>
                </div>
                <input type="hidden" name="is_deliver" value="1">
                <?= edit_form_input('order_no', set_value('order_no'), '订单号') ?>
                <?= edit_form_dropdown('shop_id', array('' => '选择账户组...') + $allShops, set_value('shop_id'), '选择账户组', "id='shop-id' style='width:100%;'") ?>
                <?= edit_form_dropdown('type_mark', array('' => '选择入库类型...') + IOSS_StaticConfig::getShippingType(), set_value('type_mark'), '入库类型', "id='type_mark'") ?>
                <?= edit_form_input('quantity', set_value('quantity'), '商品数量') ?>
                <?= edit_form_input('amount', set_value('amount'), '商品总价', 'required') ?>
                <?= edit_form_dropdown('location', array('' => '选择仓库...') + IOSS_StaticConfig::getShippingWarehouse(), set_value('location'), '仓库', "id='location'") ?>
                <?= edit_form_dropdown('area', array('' => '选择存储区...') + IOSS_StaticConfig::getShippingBlock(), set_value('area'), '所在区', "id='area'") ?>
                <div class="form-group">
                    <label class="col-xs-3 control-label">采购时间</label>
                    <div class="col-xs-7">
                        <div class="input-group input-large date-picker input-daterange" data-date="" data-date-format="yyyy-mm-dd">
                            <input type="text" class="form-control" required name="purchase_time" value="<?= set_value('purchase_time') ?>" placeholder="采购时间">
                        </div>
                    </div>
                </div>
                <?= edit_form_textarea('remark', set_value('remark'), '备注信息') ?>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="submit" class="btn btn-primary ajaxSub">保存</button>
            </div>
            <?= form_close() ?>
        </div>
    </div>
</div>

<!-- END PAGE HEADER-->
<script type="text/javascript">
    $('#deliver').click(function () {
        var ids_form = $("#app-ids");
        var ids = ids_form.serializeArray();
        if (!ids.length) {
            toastr.error('请选择要导出的申请记录！', '失败');
            return false;
        }
        ids_form.submit();
    });
    $('#merge_all').click(function () {
        if ($(this).prop("checked") == true) {
            $(".mbox").prop('checked', true);
            $(".mbox").parent('span').addClass('checked');
            return;
        } else {
            $(".mbox").prop('checked', false);
            $(".mbox").parent('span').removeClass('checked');
            return;
        }
    });

    function defConfirm(url) {
        if (!confirm("作废后不可撤回，确定要作废吗？")) {
            return false;
        }
        window.location.href = url;
        return true;
    }

    $(document).ready(function () {
        $("#reset").click(function () {
            var SelectArr = $("select");
            var SelectShop = $("select[name='shop_id']");
            var SelectProduct = $("select[name='product_id']");
            $("input[type='text']").val('');
            SelectArr.val('');

            var shop = SelectShop[0].options[0].text;
            var product = SelectProduct[0].options[0].text;
            var v_span = $('.select2-chosen');
            v_span.eq(0).html(shop);
            v_span.eq(1).html(product);

            for (var i = 0; i < SelectArr.length; i++) {
                var opt = SelectArr[i].options[0];
                opt.selected = true;

            }
        });
    });
</script>